/*
 * @Description: 模型填写
 * @Author: chenju
 * @Date: 2022-02-11 18:00:13
 * @LastEditors: chenju
 * @LastEditTime: 2023-01-18 15:24:49
 */
import { get, post, getBlob } from '@/utils/http';
import {
  StageListItem,
  GetListBomTreeResult,
  GetNodeDetailResult,
  GetBomProductionDataResult,
  GetPageItemModelParams,
  GetPageItemModelResult,
  AddNodeParams,
  EditNodeParams,
  GetListProcessInfoParams,
  GetListProcessInfoResult,
  PostUpdateProductParams,
  ProcessListItem,
  AddProcessParams,
  EditProcessParams,
  GetNodeInfoResult,
  AddStageFlowParams,
  EditStageFlowParams,
  AddTransportStageFlowParams,
  EditTransportStageFlowParams,
  IFactorListParams,
  RelationFactorListResult,
  IFlowListItem,
  IFlowParams,
  ICalcResult,
  INodeTree,
  GetPageItemFormParams,
  IProgress,
  IMaterial,
  IFillTaskParams,
  IFillTaskDetailListItem,
  IRootQueryParams
} from './model/modelDetailModel';
import { BasicPagingListResult } from '@/apis/basicModel';
import { ProcessStageCodeEnum } from '@/constants/pcf';
import config from '@/config';

enum Api {
  /* 模型详情 */
  // 查看模型总览信息
  GET_MODEL_PROCESS_DETAIL = '/footprint/modelInfo/getStageDataInfo',

  /* 原材料获取与加工 */
  // 查询模型产品BOM清单
  GET_LIST_BOM_TREE = '/footprint/modelBom/listBomTree',
  // 清空BOM数据
  CLEAR_DATA = '/footprint/modelBom/clearData',
  // 获取BOM节点详情
  GET_NODE_DETAIL = '/footprint/modelBom/nodeDetail',
  // 查看节点场地数据
  GET_BOM_PRODUCTION_DATA = '/footprint/modelBom/getBomProductionData',
  // 分页查询零部件模型
  GET_PAGE_ITEM_MODEL = '/footprint/modelInfo/pageItemModel',
  // 替换为子模型【直接替换BOM数据】
  POST_REPLACE_MODEL = '/footprint/modelBom/replaceModel',
  // 替换为替换供应商数据集
  POST_REPLACE_FORM = '/footprint/modelBom/replaceForm',
  // 新增物料
  ADD_NODE = '/footprint/modelDetail/nodeData/add',
  // 编辑物料
  EDIT_NODE = '/footprint/modelBom/modifyNode',
  // BOM新增同级、子级节点
  CREATE_NODE = '/footprint/modelBom/saveItemPiece',
  // 删除物料
  DELETE_NODE = '/footprint/modelBom/removeNode',
  // 物料编辑提交前校验单位
  CHECK_UNIT_BEFORE_EDIT_SUBMIT = '/footprint/modelProcessData/validFactorUnitAndUnit',
  // 导出产品BOM
  EXPORT_MODEL_BOM = '/footprint/modelBom/export',
  // 新增零部件模型
  CREATE_PARTS_MODEL = '/footprint/modelProcessData/createPartsModel',
  // 查询模型所有材料
  GET_MATERIAL_NAME_LIST = '/footprint/modelBom/listMaterialName',

  /* 生产过程/物流运输/使用场景/处置情景） */
  // 查询生产过程/物流运输/使用场景/处置情景信息
  GET_LIST_PROCESS_INFO = '/footprint/modelProcessInfo/listProcess',
  // 查询生产过程/物流运输/使用场景/处置情景信息节点信息
  GET_NODE_INFO = '/footprint/modelProcessData/getNodeInfo',
  // 更新产出物数量和单位
  UPDATE_PRODUCT = '/footprint/modelProcessInfo/updateProduct',
  // 新增工序/情景（生产过程/物流运输/使用场景/处置情景）
  ADD_PROCESS = '/footprint/modelProcessInfo/addProcess',
  // 编辑工序/情景（生产过程/物流运输/使用场景/处置情景）
  EDIT_PROCESS = '/footprint/modelProcessInfo/editProcess',
  // 删除工序/情景（生产过程/物流运输/使用场景/处置情景）
  DELETE_PROCESS = '/footprint/modelProcessInfo/deleteProcess',
  // 新增流（生产过程/使用场景/处置情景）
  ADD_STAGE_FLOW = '/footprint/modelFlow/addFlow',
  // 编辑流（生产过程/使用场景/处置情景）
  EDIT_STAGE_FLOW = '/footprint/modelFlow/editFlow',
  // 删除流/运输路线（生产过程/物流运输/使用场景/处置情景）
  DELETE_STAGE_FLOW = '/footprint/modelFlow/deleteFlow',
  // 新增运输路线（物流运输）
  ADD_TRANSPORT_STAGE_FLOW = '/footprint/modelTransfer/addTransfer',
  // 编辑运输路线（物流运输）
  EDIT_TRANSPORT_STAGE_FLOW = '/footprint/modelTransfer/editTransfer',
  // 查询模型BOM树
  GET_BOM_TREE = '/footprint/modelBom/getBomTree',
  // 查询节点重量
  GET_NODE_WEIGHT = '/footprint/modelBom/getNodeWeight',
  // 获取关联因子
  GET_RELATION_FACTOR = '/footprint/factor/getRelationFactor',
  // 获取流列表
  GET_FLOW_LIST = '/footprint/modelProcessData/pageData',
  // 查询因子信息
  QUERY_FACTOR_INFO = '/footprint/factor/getByFactorNo',
  // 忽略不可得
  UPDATE_IGNORE = '/footprint/modelProcessData/updateIgnore',
  // 批量忽略不可得
  BATCH_UPDATE_IGNORE = '/footprint/modelProcessData/updateBatchIgnore',
  // 查询核算结果
  QUERY_ACCOUNT_RESULT = '/footprint/model/calc/getAccountResult',
  // 核算校验
  CALC_ACCOUNT = '/footprint/model/calc/accountingModel',
  // 查看节点层级树
  GET_NODE_PATH = '/footprint/modelProcessData/getNodePath',
  // 分页查询供应商可替换的数据表
  GET_fORM_LIST = '/footprint/group/form/page',

  // 智能匹配
  SMART_RELATE = '/datamatch/modelProcessData/smartRelateFactor',
  // SMART_RELATE = '/footprint/modelProcessData/smartRelateFactor',
  // 查询材料部件数量
  GET_BOM_COUNT = '/footprint/bomProduct/countData',
  // 从产品BOM集加载BOM
  LOAD_BOM = '/footprint/modelBom/pullProductBom',
  // 按PCR填充BOM
  PCR_LOAD_BOM = '/footprint/modelBom/fillBomForPcr',
  // 统计模型数据
  GET_COUNT_BOM_NUM = '/footprint/modelBom/countBomNum',
  //  获取匹配bom进度
  GET_BOM_PROGRESS = '/footprint/modelBom/getPullProgress',
  // 查询能源燃料列表
  GET_MATERIAL_LIST = '/footprint/modelProcessData/queryFillTaskSourceMaterialList',
  //查询已填报的能源填报数据列表
  QUERY_DATA_LIST = '/footprint/modelProcessData/queryFillTaskDetailList',

  //批量忽略活动数据小于0.01的
  BATCH_IGNORE_LESS_THEN_001 = '/footprint/modelProcessData/batchIgnoreByData',
  // 设施位置列表
  LIST_FACILITY_LOCATION = '/inventory/emissionInventory/facility/listFacilityLocation',

  // 查询节点本身及子集
  QUERY_INFO_AND_CHILD = '/footprint/modelProcessData/getNodeInfoAndChildrenList',
  // 根据根节点查询BOM
  QUERY_BOM_BY_ROOT_NO = '/footprint/modelBom/listBomByRootNoV2',
  // 查询BOM根节点列表
  QUERY_ROOT_BOM = '/footprint/modelBom/listRootBom'
}

export const queryChildBomByNo = (params: any): Promise<GetListBomTreeResult[]> =>
  get(Api.QUERY_BOM_BY_ROOT_NO, params);

export const queryRootBom = (params: IRootQueryParams): Promise<GetListBomTreeResult[]> =>
  get(Api.QUERY_ROOT_BOM, params);

export const queryInfoAndChild = (modelNo: string, nodeNo: string): Promise<any> =>
  get(Api.QUERY_INFO_AND_CHILD, { nodeNo, modelNo });

/* 模型详情 */

/**
 * 查看模型详情(总览)
 */
export const getModelProcessDetail = (
  modelNo: string,
  lifeCycleStage?: keyof typeof ProcessStageCodeEnum
): Promise<StageListItem[]> => get(Api.GET_MODEL_PROCESS_DETAIL, { modelNo, lifeCycleStage });

/* 原材料获取与加工 */
/**
 * 导入产品BOM
 */

export const importModelBomAction = config.BASE_URL + '/footprint/modelBom/import';

/**
 * 查询模型产品BOM清单
 */
export const getListBomTree = (
  modelNo: string,
  parentNodeNo: string,
  lifeCycleStage?: string
): Promise<GetListBomTreeResult[]> =>
  get(Api.GET_LIST_BOM_TREE, { modelNo, parentNodeNo, lifeCycleStage });

/**
 * 清空BOM数据
 */
export const postClearData = (modelNo: string) => post(Api.CLEAR_DATA, { modelNo });

/**
 * 获取BOM节点详情
 */
export const getNodeDetail = (nodeNo: string): Promise<GetNodeDetailResult> =>
  get(Api.GET_NODE_DETAIL, { nodeNo });

/**
 * 查看节点场地数据
 */
export const getBomProductionData = (nodeNo: string): Promise<GetBomProductionDataResult> =>
  get(Api.GET_BOM_PRODUCTION_DATA, { nodeNo });

/**
 * 分页查询零部件模型
 */
export const getPageItemModel = (
  params: GetPageItemModelParams
): Promise<BasicPagingListResult<GetPageItemModelResult>> => get(Api.GET_PAGE_ITEM_MODEL, params);

/**
 * 分页查询供应商可替换的数据表
 */
export const getFormList = (
  params: GetPageItemFormParams
): Promise<BasicPagingListResult<GetPageItemModelResult>> => get(Api.GET_fORM_LIST, params);

/**
 * 替换为子模型【直接替换BOM数据】
 * @param nodeNo 物料节点编号
 * @param modelNo 需要替换的模型编号
 * @returns
 */
export const postReplaceModel = (nodeNo: string, modelNo: string) =>
  post(Api.POST_REPLACE_MODEL, { nodeNo, modelNo });

export const postReplaceForm = (nodeNo: string, formNo: string) =>
  post(Api.POST_REPLACE_FORM, { nodeNo, formNo });

/**
 * 新增物料
 */
export const addNode = (params: AddNodeParams) => post(Api.ADD_NODE, params);

/**
 * 编辑物料
 */
export const editNode = (params: EditNodeParams) => post(Api.EDIT_NODE, params);

/**
 * BOM新增同级、子级节点
 * @param nodeNo
 * @returns
 */
export const createNode = (params: EditNodeParams) => post(Api.CREATE_NODE, params);

/**
 * 删除物料
 */
export const deleteNode = (nodeNo: string) => post(Api.DELETE_NODE, { nodeNo });

/**
 * 物料编辑提交前校验单位
 */
export const postCheckUnitBeforeEditSubmit = (nodeNo: string, unit: string) =>
  post(Api.CHECK_UNIT_BEFORE_EDIT_SUBMIT, { nodeNo, unit });

/**
 * 导出BOM清单
 */
export const exportModelBom = (modelNo: string) => getBlob(Api.EXPORT_MODEL_BOM, { modelNo });

/**
 * 新增零部件模型
 */
export const createPartsModel = (modelNo: string, nodeNo: string) =>
  post(Api.CREATE_PARTS_MODEL, { modelNo, nodeNo });

/**
 * 查询模型所有材料
 */
export const getMaterialNameList = (modelNo: string): Promise<string[]> =>
  get(Api.GET_MATERIAL_NAME_LIST, { modelNo });

/* 生产过程/物流运输/使用场景/处置情景 */
/**
 * 查询生产过程/物流运输/使用场景/处置情景信息
 */
export const getListProcessInfo = (
  params: GetListProcessInfoParams
): Promise<GetListProcessInfoResult> => get(Api.GET_LIST_PROCESS_INFO, params);

/**
 * 更新产出物数量和单位
 */
export const postUpdateProduct = (params: PostUpdateProductParams): Promise<boolean> =>
  post(Api.UPDATE_PRODUCT, params);

/**
 * 新增工序/情景
 */
export const addProcess = (
  params: AddProcessParams
): Promise<Omit<ProcessListItem, 'processDataList'>> => post(Api.ADD_PROCESS, params);

/**
 * 编辑工序/情景
 */
export const editProcess = (params: EditProcessParams) => post(Api.EDIT_PROCESS, params);

/**
 * 删除工序/情景
 */
export const deleteProcess = (processNo: string) => post(Api.DELETE_PROCESS, { processNo });

/**
 * 查询生产过程/物流运输/使用场景/处置情景信息节点信息
 */
export const getNodeInfo = (nodeNo: string): Promise<GetNodeInfoResult> =>
  get(Api.GET_NODE_INFO, { nodeNo });

/**
 * 新增流（生产过程/使用场景/处置情景）
 */
export const addStageFlow = (params: AddStageFlowParams) => post(Api.ADD_STAGE_FLOW, params);

/**
 * 编辑流（生产过程/使用场景/处置情景）
 */
export const editStageFlow = (params: EditStageFlowParams) => post(Api.EDIT_STAGE_FLOW, params);

/**
 * 删除流/运输路线（生产过程/物流运输/使用场景/处置情景）
 */
export const deleteStageFlow = (nodeNo: string) => post(Api.DELETE_STAGE_FLOW, { nodeNo });

/**
 * 新增运输路线（物流运输）
 */
export const addTransportStageFlow = (params: AddTransportStageFlowParams) =>
  post(Api.ADD_TRANSPORT_STAGE_FLOW, params);

/**
 * 编辑运输路线（物流运输）
 */
export const editTransportStageFlow = (params: EditTransportStageFlowParams) =>
  post(Api.EDIT_TRANSPORT_STAGE_FLOW, params);

/**
 * 查询模型BOM树
 */
export const getBomTreeInTransport = (modelNo: string) => get(Api.GET_BOM_TREE, { modelNo });

/**
 * 查询节点重量
 */
export const getNodeWeight = (
  nodeNo: string
): Promise<{
  weight: number;
  weightUnit: string;
}> => get(Api.GET_NODE_WEIGHT, { nodeNo });

/**
 * 获取关联因子列表
 */
export const relationFactorList = (
  params: IFactorListParams
): Promise<BasicPagingListResult<RelationFactorListResult>> => get(Api.GET_RELATION_FACTOR, params);

export const getFlowList = (params: IFlowParams): Promise<BasicPagingListResult<IFlowListItem>> =>
  get(Api.GET_FLOW_LIST, params);

/**
 * 查询因子详情
 */

export const getFactorInfo = (factorNo: string): Promise<IFlowListItem> =>
  get(Api.QUERY_FACTOR_INFO, { factorNo });

/**
 * 忽略不可得
 */
export const updateIgnore = (nodeNo: string): Promise<void> => post(Api.UPDATE_IGNORE, { nodeNo });
export const batchUpdateIgnore = (nodeNoList: string[]): Promise<void> =>
  post(Api.BATCH_UPDATE_IGNORE, { nodeNoList });
export const getAccountResult = (modelNo: string): Promise<ICalcResult> =>
  get(Api.QUERY_ACCOUNT_RESULT, { modelNo });

export const accountingModel = (modelNo: string): Promise<void> =>
  post(Api.CALC_ACCOUNT, { modelNo });

export const getNodePath = (nodeNo: string): Promise<INodeTree[]> =>
  get(Api.GET_NODE_PATH, { nodeNo });

export const smartRelate = (modelNo: string): Promise<void> => post(Api.SMART_RELATE, { modelNo });

export const getBomCount = (productModel: string, enterpriseNo: string): Promise<number> =>
  get(Api.GET_BOM_COUNT, { productModel, enterpriseNo });

export const loadBomData = (params: { modelNo: string; productModel: string }): Promise<void> =>
  post(Api.LOAD_BOM, params);

export const loadBomDataByPcr = (modelNo: string): Promise<void> =>
  post(Api.PCR_LOAD_BOM, { modelNo });

export const getCountBomNum = (modelNo: string): Promise<number> =>
  get(Api.GET_COUNT_BOM_NUM, { modelNo });

export const getBomProgress = (modelNo: string): Promise<IProgress> =>
  get(Api.GET_BOM_PROGRESS, { modelNo });

export const getMaterialList = (orgCode: string, inventoryYear: string): Promise<IMaterial[]> =>
  get(Api.GET_MATERIAL_LIST, { orgCode, inventoryYear });

export const queryFillTaskDetailList = (
  params: IFillTaskParams
): Promise<IFillTaskDetailListItem[]> => get(Api.QUERY_DATA_LIST, params);

/**
 * 批量忽略活动数据小于0.01的
 * @param modelNo
 * @returns
 */
export const batchIgnoreByData = (modelNo: string) =>
  post(Api.BATCH_IGNORE_LESS_THEN_001, { modelNo });

/**
 * 设施位置列表
 * @param organizationCode
 * @param inventoryYear
 * @returns
 */
export const getListFacilityLocation = (
  organizationCode: string,
  inventoryYear: string
): Promise<string[]> => get(Api.LIST_FACILITY_LOCATION, { organizationCode, inventoryYear });
